package com.md5;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 

 

public class myThread implements Runnable{
	public String filePath;
	  Log log = LogFactory.getLog( this .getClass());   
	//读取text文本
	public static ArrayList<String> readTxt(String filePath) throws  Exception {
	 
		ArrayList<String> list = new ArrayList<String>();
		 
			FileReader read = new FileReader(filePath);
			BufferedReader br = new BufferedReader(read);
			String row;
		 	StringBuffer str=new StringBuffer("");
			 
			while ((row = br.readLine()) != null) {
			 	//list.add(row);
				str.append(row+",");
			}
			String strs=str.toString();
			String[] each = strs.split(","); 
		 
			for (String a2 : each) {
				if(!a2.equals("")){
					list.add(a2);
				}
			}
		return list;
	}
	//写入文件
	public static void writetext(ArrayList<String> list,String path) throws  Exception{
		 FileWriter fileWriter=new FileWriter(path);
		for (String a : list) {
			if(a!=null){
				 fileWriter.write(a+"\r\n");
				 fileWriter.flush();
			}
		}
	}
	
	//读取文件夹下的文件
	public static ArrayList<String> readpage(String filePath) throws  Exception{
		
		ArrayList<String> list = new ArrayList<String>();
		java.io.File file=new java.io.File(filePath); 
		String[] allpath = file.list();
		 
		for (String c : allpath) {
			FileReader read = new FileReader(filePath+"/"+c);
			BufferedReader br = new BufferedReader(read);
			String row;
		 	StringBuffer str=new StringBuffer("");
			 
			while ((row = br.readLine()) != null) {
			 	//list.add(row);
				str.append(row+",");
			}
			String strs=str.toString();
			String[] each = strs.split(","); 
		 
			for (String a2 : each) {
				if(!a2.equals("")){
					list.add(a2);
				}
			}
		}
		return list;
	}

	
	//读取指定目录下的每个tet文件 ，并对数据库进行比对,最后每个都写入以此文件名+end的目录
	public   void run() {
		log.debug("start -------start");
		long start = System.currentTimeMillis();
		filePath="D:/123";
		java.io.File file=new java.io.File(filePath); 
		String[] allpath = file.list();
		//读配置文件
		OrclBean bean = ReadPro.getorcle();
		String tablename = bean.getTablename();   //表名   
		String ziduan = bean.getZiduan();         //主键字段    
		String zhangshu=bean.getZhangshu();    	  //图片的张数	
	   String logwenjian = bean.getLogwenjian();  //log存放的文件夹
	   String  meiyouwenjian= bean.getMeiyouwenjian();  //缺少的文件所在的文件夹
		Connection con = DBPool.getInstance().getConnection(); 
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			for (String c : allpath) {
				ArrayList<String> loglist = new ArrayList<String>();    //日志list
				ArrayList<String> nolist = new ArrayList<String>();    //没有的数据nolist
				FileReader read = new FileReader(filePath+"/"+c);     //其中一个文件
				BufferedReader br = new BufferedReader(read);
				String row;
				String sql="select  "+zhangshu+"  from "+tablename+" where "+ziduan+"=?";  //把sql放到外面
				ps = con.prepareStatement(sql);
				while ((row = br.readLine())!= null) {
					//与数据库进行比对
				  if(!row.equals("")){
					  String[] myrow = row.split(",");
					  //有2条数据
	 			        if(myrow.length>1){
						// String sql="select  "+zhangshu+"  from "+tablename+" where "+ziduan+"='"+myrow[0].trim()+"'";
	 			        
						 //用预编译
				 				
				 				ps.setString(1, myrow[0].trim());
				 				  rs = ps.executeQuery();
				 				 int shuliang=0;
				 					while(rs.next()){
				 						  shuliang = rs.getInt(zhangshu);
				 					}
						if(shuliang==0){
							//数据库里数据没有在log上输入wu
							loglist.add(row.trim()+",wu");
							//数据库里没有的话写入缺少的数据表
							nolist.add(row.trim()+",meiyou");
						}else{
							//数据库里数据吻合数据日志表 
							if(shuliang==(Integer.parseInt(myrow[1].trim()))){
								//如果张数也符合后面就跟上yes
								loglist.add(row.trim()+",you,yes");
							}else{
								//张数不符合在log表后面跟上no
								loglist.add(row.trim()+",you,no");
								//张数不符合在meiyou记录上数据库的张数
								 nolist.add(row.trim()+","+shuliang);
							}
						}
						//ps.close();
						rs.close();
	 			        }
				  }
				}
				//在此循环中将list写入对应的log文本,并在后面加入标志 
				//先创建log文件夹
				 File  logfile = new File(logwenjian); 
				  logfile.mkdirs();
				 FileWriter logWriter=new FileWriter(logwenjian+"log"+c);
					for (String a : loglist) {
						if(a!=null){
							logWriter.write(a+"\r\n");
							logWriter.flush();
						}
					}
					logWriter.close();//关闭log写入流
					//在此循环中将没有的写入对应的没有文本
					 //先创建meiyou文件夹
					  File  meiyoufile = new File(meiyouwenjian); 
					  meiyoufile.mkdirs();
					 FileWriter noWriter=new FileWriter(meiyouwenjian+"meiyou"+c);
						for (String a : nolist) {
							if(a!=null){
								noWriter.write(a+"\r\n");
								noWriter.flush();
							}
						}
					noWriter.close();//关闭no写入流	
					br.close();  //关闭读取流
					//最后需要把文件删除
				  File deletefile =new File(filePath+"/"+c);
				  if(deletefile.exists()){
					  deletefile.delete();
				  }
			}
			
			con.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			System.out.println(111);
			try{
			if(null != rs){
				rs.close();
			}
			if(null != ps ){
				ps.close();
			}
			}catch(Exception e){
				
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("———————————————————————执行时间：" + (end - start)
				+ "毫秒");
		
	}
	
	
	
	
	//测试
	public static void main(String[] args) throws Exception {
	       //myThread.readandwrite("d:/ceshiwenjian");
		
		 myThread mythread=new myThread();
		   Thread t=new Thread(mythread);
		   t.start();
	}

	
	
}
